package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 消息统计VO - 遵循单一职责原则
 * 只负责消息统计信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "消息统计VO")
public class ChatMessageStatisticsVO {

    @Schema(description = "统计ID")
    private String id;

    @Schema(description = "消息ID")
    private String messageId;

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "群组ID")
    private String groupId;

    @Schema(description = "点赞数")
    private Integer likeCount;

    @Schema(description = "回复数")
    private Integer replyCount;

    @Schema(description = "转发数")
    private Integer forwardCount;

    @Schema(description = "阅读数")
    private Integer readCount;

    @Schema(description = "分享数")
    private Integer shareCount;

    @Schema(description = "收藏数")
    private Integer favoriteCount;

    @Schema(description = "举报数")
    private Integer reportCount;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "消息内容（用于显示）")
    private String messageContent;

    @Schema(description = "消息类型")
    private Integer messageType;

    @Schema(description = "发送者名称")
    private String senderName;

    @Schema(description = "群组名称")
    private String groupName;

    @Schema(description = "消息发送时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime messageSendTime;

    /**
     * 获取总互动数
     * 
     * @return 总互动数
     */
    public Integer getTotalInteractions() {
        int total = 0;
        if (likeCount != null) total += likeCount;
        if (replyCount != null) total += replyCount;
        if (forwardCount != null) total += forwardCount;
        if (shareCount != null) total += shareCount;
        if (favoriteCount != null) total += favoriteCount;
        return total;
    }

    /**
     * 获取安全的点赞数
     * 
     * @return 点赞数
     */
    public Integer getSafeLikeCount() {
        return likeCount != null ? likeCount : 0;
    }

    /**
     * 获取安全的回复数
     * 
     * @return 回复数
     */
    public Integer getSafeReplyCount() {
        return replyCount != null ? replyCount : 0;
    }

    /**
     * 获取安全的转发数
     * 
     * @return 转发数
     */
    public Integer getSafeForwardCount() {
        return forwardCount != null ? forwardCount : 0;
    }

    /**
     * 获取安全的阅读数
     * 
     * @return 阅读数
     */
    public Integer getSafeReadCount() {
        return readCount != null ? readCount : 0;
    }

    /**
     * 获取安全的分享数
     * 
     * @return 分享数
     */
    public Integer getSafeShareCount() {
        return shareCount != null ? shareCount : 0;
    }

    /**
     * 获取安全的收藏数
     * 
     * @return 收藏数
     */
    public Integer getSafeFavoriteCount() {
        return favoriteCount != null ? favoriteCount : 0;
    }

    /**
     * 获取安全的举报数
     * 
     * @return 举报数
     */
    public Integer getSafeReportCount() {
        return reportCount != null ? reportCount : 0;
    }

    /**
     * 计算互动率（相对于阅读数）
     * 
     * @return 互动率（百分比）
     */
    public Double getInteractionRate() {
        if (readCount == null || readCount == 0) {
            return 0.0;
        }
        return (double) getTotalInteractions() / readCount * 100;
    }

    /**
     * 计算点赞率（相对于阅读数）
     * 
     * @return 点赞率（百分比）
     */
    public Double getLikeRate() {
        if (readCount == null || readCount == 0) {
            return 0.0;
        }
        return (double) getSafeLikeCount() / readCount * 100;
    }

    /**
     * 计算回复率（相对于阅读数）
     * 
     * @return 回复率（百分比）
     */
    public Double getReplyRate() {
        if (readCount == null || readCount == 0) {
            return 0.0;
        }
        return (double) getSafeReplyCount() / readCount * 100;
    }

    /**
     * 计算转发率（相对于阅读数）
     * 
     * @return 转发率（百分比）
     */
    public Double getForwardRate() {
        if (readCount == null || readCount == 0) {
            return 0.0;
        }
        return (double) getSafeForwardCount() / readCount * 100;
    }

    /**
     * 获取热度等级
     * 
     * @return 热度等级（1-5星）
     */
    public Integer getPopularityLevel() {
        int total = getTotalInteractions();
        if (total >= 1000) {
            return 5;
        } else if (total >= 500) {
            return 4;
        } else if (total >= 100) {
            return 3;
        } else if (total >= 20) {
            return 2;
        } else if (total > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 获取热度等级名称
     * 
     * @return 热度等级名称
     */
    public String getPopularityLevelName() {
        int level = getPopularityLevel();
        switch (level) {
            case 5:
                return "超级热门";
            case 4:
                return "非常热门";
            case 3:
                return "热门";
            case 2:
                return "一般";
            case 1:
                return "冷门";
            default:
                return "无互动";
        }
    }

    /**
     * 检查是否为热门消息
     * 
     * @return 是否为热门消息
     */
    public boolean isPopular() {
        return getPopularityLevel() >= 3;
    }

    /**
     * 检查是否有负面反馈
     * 
     * @return 是否有负面反馈
     */
    public boolean hasNegativeFeedback() {
        return reportCount != null && reportCount > 0;
    }

    /**
     * 获取消息类型名称
     * 
     * @return 消息类型名称
     */
    public String getMessageTypeName() {
        if (messageType == null) {
            return "未知";
        }
        switch (messageType) {
            case 1:
                return "文本";
            case 2:
                return "图片";
            case 3:
                return "语音";
            case 4:
                return "视频";
            case 5:
                return "文件";
            case 6:
                return "位置";
            case 7:
                return "链接";
            default:
                return "其他";
        }
    }

    /**
     * 获取简短的消息内容
     * 
     * @param maxLength 最大长度
     * @return 简短内容
     */
    public String getShortMessageContent(int maxLength) {
        if (messageContent == null || messageContent.trim().isEmpty()) {
            return "[" + getMessageTypeName() + "]";
        }
        if (messageContent.length() <= maxLength) {
            return messageContent;
        }
        return messageContent.substring(0, maxLength) + "...";
    }

    /**
     * 获取统计摘要
     * 
     * @return 统计摘要
     */
    public String getStatisticsSummary() {
        return String.format("👍%d 💬%d 📤%d 👁%d", 
                getSafeLikeCount(), getSafeReplyCount(), 
                getSafeForwardCount(), getSafeReadCount());
    }

    /**
     * 检查是否需要关注（异常数据）
     * 
     * @return 是否需要关注
     */
    public boolean needsAttention() {
        // 举报数过多
        if (reportCount != null && reportCount > 10) {
            return true;
        }
        // 互动率异常低（阅读数很高但互动很少）
        if (readCount != null && readCount > 100 && getInteractionRate() < 1.0) {
            return true;
        }
        return false;
    }

    /**
     * 获取关注原因
     * 
     * @return 关注原因
     */
    public String getAttentionReason() {
        if (reportCount != null && reportCount > 10) {
            return "举报数过多";
        }
        if (readCount != null && readCount > 100 && getInteractionRate() < 1.0) {
            return "互动率异常低";
        }
        return "";
    }
}
